package thread;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;

/**
 * @classname:NovelDownload
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/18 22:43
 * @version: 0.1
 * @since:1.8
 */
public class NovelDownload {
    private NovelDownload() {
    }

    public static void download(String novelPath, String targetPath) {
        Objects.requireNonNull(novelPath);

        try (
                BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(novelPath).openStream()));
                BufferedWriter writer = new BufferedWriter(new FileWriter(targetPath));
        ) {
            String content = "";
            while ((content = reader.readLine()) != null) {
                if (content.contains("<div class=\"read-content j_readContent\" id=\"\">")) {
                    content = reader.readLine();
                    content = content.replaceAll("[<p>]", "\n");
                    writer.write(content);
                    break;
                }
            }

            System.out.println(Thread.currentThread().getName()+"下载成功");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
